<!--
 * @Description: Stay hungry,Stay foolish
 * @Author: Huccct
 * @Date: 2023-05-19 17:38:16
 * @LastEditors: Huccct
 * @LastEditTime: 2023-05-23 22:02:06
-->
<script setup lang="ts">
import {
  computed,
  reactive,
  ref,
  Ref,
} from 'vue'

import { ElNotification } from 'element-plus'
import {
  useRoute,
  useRouter,
} from 'vue-router'

import Identify from '@/components/VerifyCode/index.vue'
import useUserStore from '@/store/modules/user'
import { getTime } from '@/utils/time'
import {
  Lock,
  User,
  Warning,
} from '@element-plus/icons-vue'

// VerifyCode import

let $router = useRouter()
let $route = useRoute()
let loading = ref(false)

const identifyCode = ref('1234')
const identifyCodes = ref('1234567890abcdefjhijklinopqrsduvwxyz')

// 重置验证码
const refreshCode = () => {
  identifyCode.value = ''
  makeCode(identifyCode, 4)
}

const makeCode = (o: Ref<any>, l: number) => {
  for (let i = 0; i < l; i++) {
    identifyCode.value +=
      identifyCodes.value[randomNum(0, identifyCodes.value.length)]
  }
}

const randomNum = (min: number, max: number) => {
  return Math.floor(Math.random() * (max - min) + min)
}

let useStore = useUserStore()
let loginForms = ref()

const loginForm = reactive({
  // username: 'admin',
  // password: '111111',
  // verifyCode: '1234',
  username: 'admin',
  password: '123456',
  // verifyCode: '1234',
})

const validatorUsername = (rule: any, value: any, callback: any) => {
  if (value.length === 0) {
    callback(new Error('请输入账号'))
  } else {
    callback()
  }
}

const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length === 0) {
    callback(new Error('请输入密码'))
  } else if (value.length < 6 || value.length > 16) {
    callback(new Error('密码应为6~16位的任意组合'))
  } else {
    callback()
  }
}

const validatorVerifyCode = (rule: any, value: any, callback: any) => {
  console.log(value, identifyCode.value)

  if (value.length === 0) {
    callback(new Error('请输入验证码'))
  } else if (value.length < 4) {
    callback(new Error('请输入正确的验证码'))
  } else if (identifyCode.value !== value) {
    callback(new Error('请输入正确的验证码'))
  } else if (identifyCode.value === value) {
    callback()
  }
}

const login = async () => {
  await loginForms.value.validate()
  loading.value = true
  try {
    await useStore.userLogin(loginForm)
    let redirect: string = $route.query.redirect as string
    // $router.push()
    $router.push('/')
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `Hi, ${getTime()}`,
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}

const rules = {
  username: [
    {
      trigger: 'change',
      validator: validatorUsername,
    },
  ],
  password: [
    {
      trigger: 'change',
      validator: validatorPassword,
    },
  ],
  // verifyCode: [
  //   {
  //     trigger: 'blur',
  //     validator: validatorVerifyCode,
  //   },
  // ],
}

// ../../assets/images/0050.png
</script>
<template>
  <div class="login_container">
    <!-- 背景动画粒子 -->
    <div class="particles">
      <div class="particle" v-for="i in 50" :key="i"></div>
    </div>
    
    <!-- 主要内容区域 -->
    <div class="login_content">
      <el-row class="login_row">
        <!-- 左侧装饰区域 -->
        <el-col :span="12" :xs="0" class="login_left">
          <div class="left_content">
            <div class="brand">
              <img src="@/assets/images/logo.jpg" alt="Logo" class="logo" />
              <h2 class="brand_title">Vue Admin</h2>
              <p class="brand_subtitle">现代化的后台管理系统</p>
            </div>
            <div class="decoration">
              <div class="circle circle-1"></div>
              <div class="circle circle-2"></div>
              <div class="circle circle-3"></div>
            </div>
          </div>
        </el-col>
        
        <!-- 右侧登录表单 -->
        <el-col :span="12" :xs="24" class="login_right">
          <div class="login_form_container">
            <div class="form_header">
              <h1 class="login_title">欢迎回来</h1>
              <p class="login_subtitle">请登录您的账户</p>
            </div>
            
            <el-form 
              :model="loginForm" 
              :rules="rules" 
              ref="loginForms"
              class="login_form"
            >
              <el-form-item prop="username" class="form_item">
                <el-input
                  :prefix-icon="User"
                  v-model="loginForm.username"
                  clearable
                  placeholder="请输入用户名"
                  size="large"
                  class="custom_input"
                ></el-input>
              </el-form-item>
              
              <el-form-item prop="password" class="form_item">
                <el-input
                  type="password"
                  :prefix-icon="Lock"
                  show-password
                  v-model="loginForm.password"
                  size="large"
                  placeholder="请输入密码"
                  clearable
                  class="custom_input"
                ></el-input>
              </el-form-item>
              
              <!-- <el-form-item prop="verifyCode" class="form_item">
                <el-input
                  :prefix-icon="Warning"
                  show-password
                  v-model="loginForm.verifyCode"
                  placeholder="VerifyCode"
                  size="large"
                  maxlength="4"
                  class="custom_input"
                >
                  <template #append>
                    <Identify :identifyCode="identifyCode" @click="refreshCode" />
                  </template>
                </el-input>
              </el-form-item> -->
              
              <el-form-item class="form_item">
                <el-button
                  :loading="loading"
                  class="login_btn"
                  type="primary"
                  size="large"
                  @click="login"
                >
                  <span v-if="!loading">登录</span>
                  <span v-else>登录中...</span>
                </el-button>
              </el-form-item>
            </el-form>
            
            <div class="form_footer">
              <p class="footer_text">© 2024 Vue Admin. All rights reserved.</p>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  
  // 粒子动画
  .particles {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: 1;
    
    .particle {
      position: absolute;
      width: 4px;
      height: 4px;
      background: rgba(255, 255, 255, 0.3);
      border-radius: 50%;
      animation: float 6s ease-in-out infinite;
      
      @for $i from 1 through 50 {
        &:nth-child(#{$i}) {
          left: random(100) * 1%;
          top: random(100) * 1%;
          animation-delay: random(6000) * 1ms;
          animation-duration: (random(3000) + 3000) * 1ms;
        }
      }
    }
  }
  
  .login_content {
    position: relative;
    z-index: 2;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .login_row {
      width: 100%;
      height: 100%;
      
      .login_left {
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(255, 255, 255, 0.1);
        backdrop-filter: blur(10px);
        border-right: 1px solid rgba(255, 255, 255, 0.2);
        
        .left_content {
          text-align: center;
          color: white;
          
          .brand {
            margin-bottom: 60px;
            
            .logo {
              width: 80px;
              height: 80px;
              border-radius: 50%;
              margin-bottom: 20px;
              box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            }
            
            .brand_title {
              font-size: 36px;
              font-weight: 700;
              margin-bottom: 10px;
              background: linear-gradient(45deg, #fff, #f0f0f0);
              -webkit-background-clip: text;
              -webkit-text-fill-color: transparent;
            }
            
            .brand_subtitle {
              font-size: 16px;
              opacity: 0.8;
              font-weight: 300;
            }
          }
          
          .decoration {
            position: relative;
            
            .circle {
              position: absolute;
              border-radius: 50%;
              background: rgba(255, 255, 255, 0.1);
              animation: pulse 3s ease-in-out infinite;
              
              &.circle-1 {
                width: 100px;
                height: 100px;
                top: -50px;
                left: -50px;
                animation-delay: 0s;
              }
              
              &.circle-2 {
                width: 60px;
                height: 60px;
                top: 20px;
                right: -30px;
                animation-delay: 1s;
              }
              
              &.circle-3 {
                width: 80px;
                height: 80px;
                bottom: -40px;
                left: 50%;
                transform: translateX(-50%);
                animation-delay: 2s;
              }
            }
          }
        }
      }
     
      .login_right {
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(255, 255, 255, 0.15);
        backdrop-filter: blur(8px);
        min-height: 100vh;
        .login_form_container {
          width: 380px;
          background: rgba(255, 255, 255, 0.95);
          border-radius: 18px;
          box-shadow: 0 8px 32px rgba(76, 110, 245, 0.15);
          padding: 40px 32px 24px 32px;
          display: flex;
          flex-direction: column;
          align-items: center;
          animation: fadeInUp 0.8s;
          .form_header {
            text-align: center;
            margin-bottom: 32px;
            .login_title {
              font-size: 32px;
              font-weight: 700;
              color: #333;
              margin-bottom: 8px;
            }
            .login_subtitle {
              font-size: 16px;
              color: #888;
              font-weight: 400;
            }
          }
          .login_form {
            width: 100%;
            .form_item {
              margin-bottom: 24px;
              .custom_input {
                border-radius: 8px;
                font-size: 16px;
              }
            }
            .login_btn {
              width: 100%;
              height: 44px;
              font-size: 18px;
              border-radius: 8px;
              font-weight: 600;
              letter-spacing: 2px;
              transition: background 0.3s;
              background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
              border: none;
              &:hover {
                background: linear-gradient(90deg, #764ba2 0%, #667eea 100%);
              }
            }
          }
          .form_footer {
            margin-top: 24px;
            text-align: center;
            .footer_text {
              color: #aaa;
              font-size: 13px;
              letter-spacing: 1px;
            }
          }
        }
      }
    }
  }
}

// 动画
@keyframes float {
  0% {
    transform: translateY(0) scale(1);
    opacity: 0.7;
  }
  50% {
    transform: translateY(-30px) scale(1.2);
    opacity: 1;
  }
  100% {
    transform: translateY(0) scale(1);
    opacity: 0.7;
  }
}

@keyframes pulse {
  0%, 100% {
    opacity: 0.5;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.1);
  }
}

@keyframes fadeInUp {
  0% {
    opacity: 0;
    transform: translateY(40px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

// 响应式
@media (max-width: 900px) {
  .login_container {
    .login_content {
      .login_row {
        .login_left {
          display: none !important;
        }
        .login_right {
          flex: 1 1 100%;
          .login_form_container {
            width: 100%;
            min-width: 0;
            padding: 32px 12px 16px 12px;
          }
        }
      }
    }
  }
}

.user-table-card {
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(76, 110, 245, 0.10);
  background: #fff;
  padding: 32px 24px 24px 24px;
  margin-top: 32px;
  margin-bottom: 32px;
  transition: box-shadow 0.3s;
}

.el-table {
  border-radius: 12px;
  overflow: hidden;
  font-size: 16px;
  th {
    background: #f5f7fa;
    color: #333;
    font-weight: 700;
  }
  tr {
    transition: background 0.2s;
    &:hover {
      background: #f0f4ff !important;
    }
  }
}

.el-button {
  border-radius: 8px !important;
  font-weight: 600;
  padding: 6px 18px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  transition: background 0.2s;
  &:hover {
    background: linear-gradient(90deg, #764ba2 0%, #667eea 100%);
    color: #fff;
  }
}

.el-pagination {
  margin-top: 18px;
  .el-pager li.active {
    background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    border-radius: 6px;
  }
}

@media (max-width: 900px) {
  .user-table-card {
    padding: 12px 2px 12px 2px;
    margin-top: 12px;
  }
  .el-table {
    font-size: 13px;
  }
}

.table-header {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 18px;
  .el-icon {
    margin-right: 8px;
    color: #667eea;
  }
  .header-title {
    letter-spacing: 1px;
  }
}

</style>
